import time

# 查询
def query():
    from atm_1.utility import query_one
    from atm_1.user import login_name
    result = query_one("select * from user_info where username = '%s'" %login_name)
    output = result[0] + "\n" + "手机号："+ result[2] + "\n" + "余额："+ str(result[3])
    print(output)
    from atm_1.menu import main_menu
    main_menu()

#  账户金额的操作
def change_balance(username, money):
    from atm_1.utility import query_one
    balance = query_one("select balance from user_info where username = '%s'" %username)
    new_balance = balance[0] + float(money)
    from atm_1.utility import db_operation
    db_operation("update user_info set balance = %f where username = '%s'" % (new_balance, username))
    if float(money) > 0:
        print("成功存款%f，剩余金额%f" % (money, new_balance))
    else:
        print("成功取款%f，剩余金额%f" % (money * -1, new_balance))

# 取款
def withdraw():
    money = input("请输入你的取款金额：")
    from atm_1.utility import query_one
    from atm_1.user import login_name
    balance = query_one("select balance from user_info where username = '%s'" %login_name)
    from atm_1.utility import check_number
    if check_number(money) and float(money) <= balance[0] and float(money) % 100 == 0:
        change_balance(login_name,float(money) * -1)
        string = time.strftime('%Y-%m-%d %H:%M:%S')+" "+"取款"+money
        from atm_1.utility import db_operation
        db_operation("insert into history1(username,content)values('%s','%s')" %(login_name,string))
        from atm_1.menu import main_menu
        main_menu()
    else:
        print("取款金额有误")
        withdraw()

# 存款
def deposite():
    money = input("请输入你的存款金额：")
    from atm_1.utility import check_number
    if check_number(money) and float(money) % 100 == 0:
        from atm_1.user import login_name
        change_balance(login_name, float(money))
        string = time.strftime('%Y-%m-%d %H:%M:%S') + " " + "存款" + money
        from atm_1.utility import db_operation
        db_operation("insert into history1(username,content)values('%s','%s')" % (login_name, string))
        from atm_1.menu import main_menu
        main_menu()
    else:
        print("存款金额有误")
        deposite()

# 转账
def transer():
    account = input("请输入你的转账账号：")
    from atm_1.utility import query_one
    account_search = query_one("select username from user_info where username = '%s'" %account)
    from atm_1.user import login_name
    if account_search != None and account != login_name:
        money = input("请输入你的转账金额：")
        from atm_1.utility import check_number
        if check_number(money):
            money = float(money)
            balance1 = query_one("select balance from user_info where username = '%s'" %login_name)
            if money <= balance1[0]:
                new_balance1 = balance1[0] - float(money)
                from atm_1.utility import db_operation
                db_operation("update user_info set balance = %f where username = '%s'" % (new_balance1, login_name))
                print("成功转账%f！当前余额为：%f" %(money,new_balance1))
                balance2 = query_one("select balance from user_info where username = '%s'" %account)
                new_balance2 = balance2[0] + float(money)
                db_operation("update user_info set balance = %f where username = '%s'" % (new_balance2, account))
                print("另一个账户余额为：%f" %new_balance2)  # 方便检查另一个账户是否有进账
                string1 = time.strftime('%Y-%m-%d %H:%M:%S') + " " + "转出" + str(money)
                db_operation("insert into history1(username,content)values('%s','%s')" % (login_name, string1))
                string2 = time.strftime('%Y-%m-%d %H:%M:%S') + " " + "转入" + str(money)
                db_operation("insert into history1(username,content)values('%s','%s')" % (account, string2))
            else:
                print("余额不足")
                transer()
        else:
            print("你输入的金额不符合规范")
            transer()
    elif account == login_name:
        print("你输入的账号为当前账号")
        transer()
    else:
        print("你输入的账号不存在")
        transer()
    from atm_1.menu import main_menu
    main_menu()

# 流水
def history():
    from atm_1.utility import query_all
    from atm_1.user import login_name
    result = query_all("select username,content from history1 where username = '%s'" %login_name)
    if result ==():
        print('当前用户流水信息为空')
    else:
        for h in result:
            print(h[0] + ' ' + h[1])
    from atm_1.menu import main_menu
    main_menu()